<?php

namespace app\dao\exam;

use app\dao\BaseDao;
use app\model\exam\ExamIllegal;
use app\model\exam\ExamMonitorList;
use app\model\exam\ExamStatus;

class ExamMonitorListDao extends BaseDao
{

    public function index(array $where)
    {
        return $this
            ->getModel()
            ->where('website_id', $where['website_id'])
            ->order('id', 'desc')
            ->paginate($where['pageSize']);
    }

    public function create(array $where)
    {
        unset($where['id']);
        return $this
            ->getModel()
            ->create($where);
    }

    public function updateDao(array $where)
    {
        unset($where['website_id']);
        return $this
            ->getModel()
            ->update($where);
    }

    public function deleteDao(array $where)
    {
        return $this
            ->getModel()
            ->whereIn('id', $where['id'])
            ->update(['delete_time' => date('Y-m-d H:i:s', time())]);
    }

    public function getUserFromId(array $where)
    {
        return $this->search()
            ->where('admin_id', $where['admin_id'])
            ->where('exam_id', $where['exam_id'])
            ->where('website_id', $where['website_id'])
            ->find();
    }

    public function examPerson(array $where)
    {
        $obj = (new ExamStatus());
        $data['allExam'] = $obj
            ->where('website_id', $where['website_id'])
            ->count();

        $data['wayExam'] = $obj
            ->where('website_id', $where['website_id'])
            ->where('exam_status', '考试中')
            ->count();
        return $data;
    }

    public function examForId(array $where)
    {
        $obj = (new ExamStatus());

        if ($where['list']) {
            return $obj
                ->with(['users', 'OfficeClass' => function ($query) {
                    $query->withField(['id', 'title']);
                }])
                ->where('exam_status', '考试中')
                ->where('website_id', $where['website_id'])
                ->where('exam_id', $where['exam_id'])
                ->paginate(9);
        }


        $data['wayExam'] = $obj //正在考试人数
        ->where('website_id', $where['website_id'])
            ->where('exam_id', $where['exam_id'])
            ->where('exam_status', '考试中')
            ->count();

        $data['referExam'] = $obj //已交卷人数
        ->where('website_id', $where['website_id'])
            ->where('exam_id', $where['exam_id'])
            ->where('exam_status', '已交卷')
            ->count();

        return $data;

    }

    public function getExamTryRoll(array $where)
    {
        return (new ExamIllegal())->where('website_id', $where['website_id'])->order('id', 'desc')->limit(20)->select();
    }

    public function getExamTryUser(array $where)
    {
        return (new ExamIllegal())
            ->where('website_id', $where['website_id'])
            ->where('idcard',$where['idcard'])
            ->order('id', 'desc')
            ->limit(5)
            ->select();
    }


    protected function setModel(): string
    {
        return ExamMonitorList::class;
    }
}